package cn.libinal.scloud.base.oauth.service;

import cn.libinal.scloud.base.common.dto.ClientDto;
import cn.libinal.scloud.base.common.dto.PermissionDto;
import cn.libinal.scloud.base.common.dto.RoleDto;
import cn.libinal.scloud.base.common.dto.UserDto;
import cn.libinal.scloud.base.common.web.ResultJson;
import cn.libinal.scloud.base.oauth.service.impl.BaseClientsFallback;
import feign.Feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Set;

/**
 * @author libin
 */
@FeignClient(name = "base", fallback  = BaseClientsFallback.class)
public interface BaseClients {
    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    @RequestMapping(name = "根据用户名查询用户", path = "user/findByUsername", method = RequestMethod.GET)
    ResultJson<UserDto> findByUsername(@RequestParam(value = "username") String username);

    /**
     * 根据用户ID查询角色
     *
     * @param userId 用户ID
     * @return 角色
     */
    @RequestMapping(name = "根据用户ID查询角色", path = "role/getRoleByUserId", method = RequestMethod.GET)
    ResultJson<Set<RoleDto>> getRoleByUserId(@RequestParam(name = "userId") String userId);

    /**
     * 根据角色获取权限
     *
     * @param roleId 角色ID
     * @return 权限
     */
    @RequestMapping(name = "根据角色获取权限", path = "permission/getPermissionByRoleId", method = RequestMethod.GET)
    ResultJson<Set<PermissionDto>> getRolePermission(@RequestParam(value = "roleId") String roleId);

    /**
     * 根据客户端Id查询客户端信息
     *
     * @param clientId 客户端ID
     * @return 客户端信息
     */
    @GetMapping(name = "根据客户端Id查询客户端信息", path = "client/findClientByClientId")
    ResultJson<ClientDto> findClientByClientId(@RequestParam(name = "clientId") String clientId);

}

@Configuration
class FooConfiguration {
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();
    }
}
